package com.oceansoft.core.jxkh.dao.impl;

import com.hui.platform.system.exception.HuiException;
import com.hui.platform.system.utility.StringUtil;
import com.oceansoft.core.common.util.PageUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.domain.SysDept;
import com.oceansoft.core.domain.SysUser;
import com.oceansoft.core.jxkh.dao.ITaskRelationDao;
import com.oceansoft.core.jxkh.domain.BeChecker;
import com.oceansoft.core.jxkh.domain.Checker;
import com.oceansoft.core.jxkh.domain.Task;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by begcoffee on 2017/12/11.
 */
@Repository
public class TaskRelationDaoImpl implements ITaskRelationDao {
    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<Checker> querybycondition(HashMap<String, String> map, SysUser user) {
        String sql = "select t.*,(select dutyname from t_sys_user t1 where t1.dbid = t.checkerid) as dutyname,(select dutylevel from t_sys_user t1 where t1.dbid = t.checkerid) as dutylevel from t_pskh_task_checker t where 1=1 and taskid ="+map.get("taskId");
        if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank((map.get("deptid")))){
            sql+=" and deptno ='"+map.get("deptid")+"'";
        }
        List<Checker> list = jdbcTemplate.query(sql, new RowMapper<Checker>() {
            @Override
            public Checker mapRow(ResultSet rs, int arg1) throws SQLException {
                Checker checker = new Checker();
                checker.setDbid(rs.getInt("DBID"));
                checker.setCheckername(rs.getString("Checkername"));
                checker.setCheckerid(rs.getString("CHECKERID"));
                checker.setTaskid(rs.getString("TASKID"));
                checker.setDeptno(rs.getString("DEPTNO"));
                checker.setDutylevel(rs.getString("DUTYLEVEL"));
                checker.setDutyname(rs.getString("DUTYNAME"));
                return checker;
            }
        });
        System.out.println(sql);
        return  list ;
    }

    @Override
    public void deleteCheck(String dbid) throws HuiException {
        String sql = "delete from t_pskh_task_checker where dbid = "+dbid;
        jdbcTemplate.update(sql);
    }

    @Override
    public List<SysUser> checkUserList(HashMap<String, String> map, SysUser user) {
        String sql = "select * from T_SYS_USER t where 1=1 ";
        sql+= " and not exists(select 1 from t_pskh_task_checker t1 where t1.CHECKERID = t.dbid and taskid ="+map.get("taskId") +" ) ";
        if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank((map.get("deptid")))){
            sql+=" and deptid='"+map.get("deptid")+"'";
        }
        List<SysUser> list = jdbcTemplate.query(sql, new RowMapper<SysUser>() {
            @Override
            public SysUser mapRow(ResultSet rs, int arg1) throws SQLException {
                SysUser user = new SysUser();
                user.setDbid(rs.getInt("DBID"));
                user.setName(rs.getString("NAME"));
                user.setPoliceNum(rs.getString("POLICENUM"));
                user.setDeptId(rs.getString("DEPTID"));
                user.setDutyName(rs.getString("DUTYNAME"));
                user.setDutyId(rs.getString("dutylevel"));
                return user;
            }
        });
        System.out.println(sql);
        return list;
    }

    @Override
    public void saveChecker(Checker checker) throws HuiException {
        String sql ="insert into t_pskh_task_checker (DBID  ,TASKID , DEPTNO   , CHECKERID ,CHECKERNAME  ) values (SEQ_PSKH_TASK_CHECKER.nextval,?,?,?,?)";
        jdbcTemplate.update(sql,checker.getTaskid(),checker.getDeptno(),checker.getCheckerid(),checker.getCheckername());
    }

    @Override
    public List<BeChecker> relationList(HashMap<String, String> map, SysUser user) throws HuiException {
        String sql = "select DBID,BECHECKERID,BECHECKERNAME,CHECKERDBID,(select policeNum from t_sys_user where dbid = t.BECHECKERID) as policenum,(select dutyname from t_sys_user t1 where t1.dbid = t.BECHECKERID) as dutyname,(select dutylevel from t_sys_user t1 where t1.dbid = t.BECHECKERID) as dutylevel  from T_PSKH_TASK_RELATION t where 1=1 ";
        if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank((map.get("checkerdbid")))){
            sql+=" and checkerdbid ="+map.get("checkerdbid");
        }
//        String count_sql = PageUtil.getCountSQL(sql.toString());
//        int _total = jdbcTemplate.queryForInt(count_sql);
//        String page_sql = PageUtil.getPageSQL(sql.toString(), offset,limit);
        List<BeChecker> list = jdbcTemplate.query(sql, new RowMapper<BeChecker>() {
            @Override
            public BeChecker mapRow(ResultSet rs, int arg1) throws SQLException {
                BeChecker checker = new BeChecker();
                checker.setDbid(rs.getInt("DBID"));
                checker.setBecheckerid(rs.getInt("BECHECKERID"));
                checker.setBecheckername(rs.getString("BECHECKERNAME"));
                checker.setCheckerdbid(rs.getInt("CHECKERDBID"));
                checker.setPoliceNum(rs.getString("POLICENUM"));
                checker.setDutylevel(rs.getString("DUTYLEVEL"));
                checker.setDutyname(rs.getString("DUTYNAME"));
                return checker;
            }
        });
        System.out.println(sql);
        return list;
    }

    @Override
    public void deleteBeCheck(String dbid) throws HuiException {
        String sql = "delete from t_pskh_task_relation where dbid = "+dbid;
        jdbcTemplate.update(sql);
    }

    @Override
    public List<SysUser> beCheckerList(HashMap<String, String> map, SysUser user) {
        String sql = "select * from T_SYS_USER t where 1=1 ";
        sql+= " and not exists(select 1 from t_pskh_task_relation t1 where t1.BECHECKERID = t.dbid AND T1.CHECKERDBID = "+map.get("checkerdbid")+") ";
        if(com.oceansoft.core.common.util.StringUtil.isNotNullAndBlank((map.get("deptid")))){
            sql+=" and deptid='"+map.get("deptid")+"'";
        }
        sql+=" and dbid <>'"+map.get("checkerdbid")+"'";
        List<SysUser> list = jdbcTemplate.query(sql, new RowMapper<SysUser>() {
            @Override
            public SysUser mapRow(ResultSet rs, int arg1) throws SQLException {
                SysUser user = new SysUser();
                user.setDbid(rs.getInt("DBID"));
                user.setName(rs.getString("NAME"));
                user.setPoliceNum(rs.getString("POLICENUM"));
                user.setDeptId(rs.getString("DEPTID"));
                user.setDutyName(rs.getString("DUTYNAME"));
                user.setDutyId(rs.getString("dutylevel"));
                return user;
            }
        });
        System.out.println(sql);
        return list;
    }

    @Override
    public void saveBeChecker(BeChecker checker) throws HuiException {
        String sql ="insert into T_PSKH_TASK_RELATION (DBID  ,CHECKERDBID , BECHECKERID   , BECHECKERNAME ,WEIGHT  ) values (SEQ_PSKH_TASK_RELATION.nextval,?,?,?,?)";
        jdbcTemplate.update(sql,checker.getCheckerdbid(),checker.getBecheckerid(),checker.getBecheckername(),"");
    }

    @Override
    public List<Map<String,Object>> getFirstLevelDept(boolean isadmin,SysUser user) {
        String sql = "select name,alias,deptid from t_sys_dept t where t.parentid='320000'";
        if(!isadmin){
            sql+=" and t.deptid = '"+user.getDeptId()+"'";
        }
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public List<Map<String, Object>> findCheckerByDeptidAndTaskId(Object deptid, String taskId) {
        String sql = "select CHECKERNAME,CHECKERID,DBID from t_pskh_task_checker t where t.taskid = "+taskId+" and t.deptno like '"+deptid+"%'";
        return jdbcTemplate.queryForList(sql);
    }

    @Override
    public List<SysUser> findBeCheckerByCheckeridAndTaskId(Object checkerid, String taskId) {
        String sql = "select t.becheckername AS name  from t_pskh_task_relation t where t.checkerdbid = "+checkerid;
        List<SysUser> list = jdbcTemplate.query(sql, new RowMapper<SysUser>() {
            @Override
            public SysUser mapRow(ResultSet rs, int arg1) throws SQLException {
                SysUser user = new SysUser();
                user.setName(rs.getString("NAME"));
                return user;
            }
        });
        return list;
    }

    @Override
    public void deleteAllBeCheck(String dbid) {
        String sql = "delete from t_pskh_task_relation where CHECKERDBID = "+dbid;
        jdbcTemplate.update(sql);
    }
}
